﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.OleDb;
using DAO;
using DTO;
using BUS;
using System.Data;
namespace QL_xxx
{
	/// <summary>
	/// Interaction logic for QLSanPham_View.xaml
	/// </summary>
	public partial class QLSanPham_View : UserControl
	{
        private List<SanPhamDTO> listSanPham = null;
        private SanPhamDTO sanPhamSelected = null;
		public QLSanPham_View()
		{
			this.InitializeComponent();
		}

        private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
        {
            listSanPham = SanPhamBUS.LayDSSanPham();
            LoadGrid(listSanPham);
        }


   
        private void LoadGrid(List<SanPhamDTO> listSanPham)
        {
            btnSua.IsEnabled = false;
            btnXoa.IsEnabled = false;
            if (listSanPham != null && listSanPham.Count > 0)
            {
                GridDSSanPham.ItemsSource = listSanPham;
                GridDSSanPham.Columns[1].Header = "Tên sản Phẩm";
                GridDSSanPham.Columns[2].Header = "Đơn giá";
                GridDSSanPham.Columns[3].Header = "Số lượng trong kho";

                GridDSSanPham.Columns[1].Width = GridDSSanPham.ActualWidth / 3;
                GridDSSanPham.Columns[2].Width =  GridDSSanPham.ActualWidth / 3;
                GridDSSanPham.Columns[3].Width = GridDSSanPham.ActualWidth / 3;

                GridDSSanPham.Columns[0].Visibility = Visibility.Hidden;
                

            }
        }

        private void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            try
            {
                GridDSSanPham.Columns[1].Width = GridDSSanPham.ActualWidth / 3;
                GridDSSanPham.Columns[2].Width = GridDSSanPham.ActualWidth / 3;
                GridDSSanPham.Columns[3].Width = GridDSSanPham.ActualWidth / 3;
            }
            catch
            {
            }
        }
        private void btnThem_Click(object sender, RoutedEventArgs e)
        {
  
            ThemSanPham them = new ThemSanPham();
            them.onThemSanPhamThanhCong +=new OnThemSanPhamThanhCong(them_onThemSanPhamThanhCong);
            them.ShowDialog();
        }
        void them_onThemSanPhamThanhCong()
        {
            listSanPham = SanPhamBUS.LayDSSanPham();
            LoadGrid(listSanPham);
        }
     

       
        private void GridDSSanPham_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            sanPhamSelected = (SanPhamDTO)GridDSSanPham.SelectedItem;
            if (sanPhamSelected != null)
            {
                btnSua.IsEnabled = true;
                btnXoa.IsEnabled = true;
            }
            else
            {
                btnSua.IsEnabled = false;
                btnXoa.IsEnabled = false;
            }
        }  
        private void btnSua_Click(object sender, RoutedEventArgs e)
        {
            SuaSanPham sua = new SuaSanPham(sanPhamSelected.MaSanPham, sanPhamSelected.TenSanPham, sanPhamSelected.SoLuongTrongKho,sanPhamSelected.GiaSanPham);
            sua.onSuaSpThanhCong += new OnSuaSanPhamThanhCong(sua_onSuaSpThanhCong);
            sua.onNhapXuatThanhCong += new OnNhapXuatSPThanCong(sua_onNhapXuatThanhCong);
            sua.ShowDialog();
        }

        void sua_onNhapXuatThanhCong()
        {
            listSanPham = SanPhamBUS.LayDSSanPham();
            LoadGrid(listSanPham);
        }

        void sua_onSuaSpThanhCong()
        {
            listSanPham = SanPhamBUS.LayDSSanPham();
            LoadGrid(listSanPham);
        }

        private void btnXoa_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBoxResult.Yes == MessageBox.Show("Bạn có chắc xóa, xóa sản phẩm sẽ dẫn tới xóa hết các thông tin liên quan!", "Cẩn thận", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No))
            {
                if (SanPhamBUS.XoaSanPham(sanPhamSelected.MaSanPham))
                {
                    MessageBox.Show("Xong");
                    listSanPham = SanPhamBUS.LayDSSanPham();
                    LoadGrid(listSanPham);
                }
                else
                    MessageBox.Show("Lỗi chưa xác định, xin liên hệ nhà cung cấp");
            }
        }


	}
}